package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.dto.TagDto;
import com.haoshui.domain.entity.Tag;
import com.haoshui.domain.vo.PageVo;
import com.haoshui.domain.vo.TagVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.mapper.TagMapper;
import com.haoshui.service.TagService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 17:17:45
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult tagList(Integer pageNum, Integer pageSize, String name, String remark) {
        LambdaQueryWrapper<Tag> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(name) && StringUtils.hasText(name),Tag::getName, name);
        queryWrapper.eq(Objects.nonNull(remark) && StringUtils.hasText(remark), Tag::getRemark, remark);
        Page<Tag> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        //封装Vo
        List<Tag> records = page.getRecords();
        PageVo pageVo = new PageVo(records, page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(TagDto tagDto) {
        Tag tag = BeanCopyUitls.copyBean(tagDto, Tag.class);
        boolean save = save(tag);
        if(save){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_FAILED);
        }
    }

    @Override
    public ResponseResult deleteTag(Long id) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getId , id);
        boolean remove = remove(queryWrapper);
        if(remove){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
        }
    }

    @Override
    public ResponseResult getTag(Long id) {
        //根据Id查找信息
        Tag tag = getById(id);

        //TODO 如果没有查到，返回错误信息或者抛出异常

        //封装Vo
        TagVo tagVo = BeanCopyUitls.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult updateTag(Tag tag) {
        //更新
        boolean b = updateById(tag);

        //对结果进行判断
        if(b){
            return ResponseResult.okResult();
        }else{
            //TODO 或者修改为抛出异常
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }
    }

    @Override
    public ResponseResult listAllTag() {

        List<Tag> tags = list();
        List<TagVo> tagVos = BeanCopyUitls.copyBeanList(tags, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }
}
